package gui.crud;

import gui.AbstractFramePythagoras;
import gui.Menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public abstract class AbstractEditPanel extends JPanel {
	
	private JButton confirmButton, deleteButton, toMenuButton;
	private JTextField nameField;
	private JLabel labelForName;
	private JComboBox<String> objectsBox;
	private JLabel objectsLabel;
	
	private String title;
	


	public AbstractEditPanel(String title) {
		this(title, 800, 600);	
	}
	
	public AbstractEditPanel(String title, int width, int height) {
		this.title = title;
		
		this.setSize(width, height);
		this.setLayout(null);
		
		this.confirmButton = new JButton("Confirmar");
		this.deleteButton = new JButton("Remover");
		this.toMenuButton = new JButton("Menu");
		this.nameField = new JTextField();
		this.labelForName = new JLabel("<html><i><b>Procurar:</b></i></html>");
		this.objectsBox = new JComboBox<String>();
		this.objectsLabel = new JLabel();
		
		this.confirmButton.setBounds((width/2) - 55, height - 140, 110, 25);
		this.deleteButton.setBounds((width/2) - 55, height - 105, 110, 25);
		this.toMenuButton.setBounds((width/2) - 55, height - 70, 110, 25);
		this.nameField.setBounds(60, 60, 120, 20);
		this.labelForName.setBounds(60, 38, 120, 20);
		this.objectsBox.setBounds(200, 60, 150, 20);
		this.objectsLabel.setBounds(200, 38,150, 20);
		
		this.confirmButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				confirmChanges();
			}
		});
		
		this.deleteButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (JOptionPane.showConfirmDialog(getThis(), "Deseja realmente remover?", "Confirme", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					deleteAction();
				}
				
			}
		});
		
		this.toMenuButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				toMenu();
			}
		});
		
		this.objectsBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				updateFields();
			}
		});
		
		
		this.add(confirmButton);
		this.add(deleteButton);
		this.add(toMenuButton);
		this.add(nameField);
		this.add(objectsBox);
		this.add(labelForName);
		this.add(objectsLabel);
		
		setUpNewComponents();
		addNewComponents();
		setObjectsTypeAndContent();
	}

	protected AbstractEditPanel getThis() {
		return this;
	}
	
	protected JComboBox<String> getObjectsBox() {
		return objectsBox;
	}

	protected JLabel getObjectsLabel() {
		return objectsLabel;
	}

	protected void toMenu() {
		Menu m = new Menu();
		((AbstractFramePythagoras) this.getTopLevelAncestor()).changePanel(m, m.getTitle());
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	protected abstract void confirmChanges();
	
	protected abstract void deleteAction();
	
	protected abstract void setUpNewComponents();
	
	protected abstract void addNewComponents();
	
	protected abstract void setObjectsTypeAndContent();
	
	protected abstract void updateFields();
	
}
